<template>
  <ele-modal
    form
    :width="600"
    :model-value="modelValue"
    :title="title"
    @update:modelValue="updateModelValue"
  >
    <ele-page>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="98px">
        <el-form-item label="还款类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio-button label="正常">正常</el-radio-button>
            <el-radio-button label="逾期费">逾期费</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" style="width: 130px">
            <template #append>元</template>
          </el-input>
        </el-form-item>

        <el-form-item label="付款时间" prop="pay_time">
          <el-date-picker
            v-model="form.pay_time"
            class="filter-item"
            type="datetime"
            placeholder="付款时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 230px"
            :clearable="false"
          />
        </el-form-item>

        <el-form-item label="付款凭证" prop="pay_images">
          <image-upload :limit="10" v-model="form.pay_images" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" :loading="loading" @click="save">
            保存
          </el-button>
        </el-form-item>
      </el-form>
    </ele-page>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, watch } from 'vue';
  import { billRepay } from '@/api/company';
  import { EleMessage } from 'ele-admin-plus/es';
  import ImageUpload from '@/components/ImageUpload/index.vue';

  import { useFormData } from '@/utils/use-form-data';

  const emit = defineEmits(['done', 'update:modelValue']);
  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };

  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    /** 修改回显的数据 */
    data: Object,
    type: String
  });

  const title = ref('');

  /** 提交状态 */
  const loading = ref(false);

  /** 表单实例 */
  const formRef = ref(null);

  /** 表单数据 */
  const [form, resetFields] = useFormData({
    id: 0,
    type: '正常',
    vehicle_id: 0,
    amount: '', // 金额
    pay_time: '', // 付款时间
    bill_cate: '', // 金额
    pay_images: '' // 付款凭证
  });

  const rules = reactive({
    amount: [
      {
        required: true,
        message: '请填写金额',
        type: 'string',
        trigger: 'blur'
      }
    ],
    pay_time: [
      {
        required: true,
        message: '请选择付款时间',
        type: 'string',
        trigger: 'blur'
      }
    ],
    pay_images: [
      {
        required: true,
        message: '请上传凭证',
        type: 'string',
        trigger: 'blur'
      }
    ]
  });

  /** 提交 */
  const save = () => {
    formRef.value?.validate?.((valid) => {
      if (!valid) {
        return;
      }
      var requestData = Object.assign({}, form);
      billRepay(requestData)
        .then(() => {
          loading.value = false;
          EleMessage.success('操作成功');
          updateModelValue(false);
          emit('done');
        })
        .catch((e) => {
          loading.value = false;
          console.log(e);
        });
    });
  };

  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        if (props.data) {
          form.amount = '';
          form.pay_images = '';
          form.id = props.data.id;
          form.bill_cate = props.data.cate;
          form.vehicle_id = props.data.vehicle_id;
        }
        title.value = props.data.plate_number + props.data.cate + '还款';
      } else {
        resetFields();
        formRef.value?.clearValidate?.();
        console.log('关闭');
      }
    }
  );
</script>
